package com.ed100.sass.patter.flyweight;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlyWeightFactory {

	private Map<Character, FlyWeight> map = new HashMap<Character, FlyWeight>();

	/**
	 * 单纯享元模式
	 * 
	 * @param state
	 * @return
	 */
	public FlyWeight factory(Character state) {
		FlyWeight fly = map.get(state);

		if (fly == null) {
			fly = new ConcreateFlyWeight(state);
			map.put(state, fly);
		}

		return fly;
	}

	public ConcreteCompositeFlyweight factory(List<Character> compositeState) {
		ConcreteCompositeFlyweight compositeFly = new ConcreteCompositeFlyweight();
		
		for(Character cha : compositeState) {
			compositeFly.add(cha, this.factory(cha));
		}
		
		return compositeFly;
	}
}
